// pages/search/search.js
const interfaces = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotKeywordList: [],
    keyword: '',
    listData: [],
    currentSortType: "default",
    currentSortOrder: "",
    historyData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getHotData()
  },
  /**
   * 热门搜索点击
   */
  searchWords(e){
    //console.log(e)
    this.inputChange(e)
  },
  /**
   * 清空搜索框内容
   */
  clear(){
    this.setData({
      keyword: ''
    })
    this.getHelpKeyword()
  },
  /**
   * 清空历史记录
   */
  clearHistory(){
    let openId = wx.getStorageSync('openId')
    let self = this
    wx.request({
      url: interfaces.clearHistory,
      method: "POST",
      data:{
        openId: openId
      },
      success(res){
        console.log(res)
        self.setData({
          historyData: []
        })
      }
    })
  },
  /**
   * 取消
   */
  cancel(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 热门搜索内容
   */
  getHotData(){
    let openId = wx.getStorageSync('openId')
    let self = this
    wx.request({
      url: interfaces.hotDetail,
      data:{
        openId: openId
      },
      success(res){
        //console.log(res)
        self.setData({
          hotKeywordList: res.data.hotKeywordList,
          historyData: res.data.historyData
        })
      }
    })
  },
  /**
   * 搜索
   */
  inputChange(e){
    // console.log(e.detail.value)
    //console.log(e)
    let value = e.detail.value;
    let word = value || e.currentTarget.dataset.keyword
    this.setData({
      keyword: word
    })
    this.getHelpKeyword()
    this.getAddHistory()
  },
  getAddHistory(){
    let openId = wx.getStorageSync('openId')
    let self = this
    wx.request({
      url: interfaces.addHistory,
      method: "POST",
      data:{
        openId: openId,
        keyword: self.data.keyword
      },
      success(res){
        self.getHotData()
      }
    })
  },
  getHelpKeyword(){
    let self = this
    wx.request({
      url: interfaces.searchList,
      data: {
        keyword: self.data.keyword
      },
      success(res) {
        //console.log(res)
        self.setData({
          listData: res.data.keywords
        })
      }
    })
  },
  /**
   * 商品列表排序
   */
  openSort(e) {
    let uid = e.currentTarget.dataset.uid;
    switch (uid) {
      case 'classify':
        this.setData({
          currentSortType: 'category',
          currentSortOrder: ""
        })
        this.getData();
        break;
      case 'tariff':
        let tmp = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmp = 'desc'
        }
        this.setData({
          currentSortType: "price",
          currentSortOrder: tmp
        })
        this.getData();
        break;
      default:
        this.setData({
          currentSortType: "default",
          currentSortOrder: ""
        });
        this.getData();
    }
  },
  getData() {
    const self = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: interfaces.newgoods,
      data: {
        isNew: 1,
        order: self.data.currentSortOrder
      },
      success(res) {
        //console.log(res)
        wx.hideLoading();
        self.setData({
          listData: res.data.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})